﻿using ASmile.Com;
using ASmile.Com.DictCom;
using ASmile.DBModel;
using ASmile.Win.Controls;
using System;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    /// <summary>
    /// 字典分类查询及维护
    /// </summary>
    public partial class FrmDict : FrmBase
    {
        public FrmDict()
        {
            InitializeComponent();
        }

        IDictComBLL BLLObj { get; } = ObjectFactory.GetObject<IDictComBLL>("DictComBLL");

        ArgDictSearch SearchArg { get; } = new ArgDictSearch();

        void RefreshData()
        {
            OnSetPageData(1, ucPageNav.OnePageCount, out int total);
            ucPageNav.InitiControl(total);
        }

        void RefreshType()
        {
            var data = BLLObj.GetDictTypes(tsTxtTypeSearch.Text);
            dgvDictType.DataSource = data;
            dgv.DataSource = null;
        }

        private void DgvDictType_SelectionChanged(object sender, DataGridViewCellEventArgs e)
        {
            RefreshData();
        }

        private void FrmDict01_Load(object sender, EventArgs e)
        {
            dgvDictType.SelectionChangedAndCellClick += DgvDictType_SelectionChanged;
            ucPageNav.PageChangedEvent += OnSetPageData;
            RefreshType();
            dgv.SetInputEdit<Dict_Common>(Dict_Common.CNDictValueOrderNo, txtOrderNo, SetOrderNo);
        }

        bool SetOrderNo(Dict_Common model, EditText txt)
        {
            var ret = BLLObj.SetSortNo(
                new ArgSetSortNo
                {
                    Id = model.Id,
                    SortNo = ((int)txtOrderNo.Value)
                });

            ShowMessage(ret.msg);
            return ret.success;
        }

        private void OnSetPageData(int pageNo, int pageSize, out int total)
        {
            var typeData = dgvDictType.GetSelectedClassData<Dict_CommonType>();
            if (typeData == null)
            {
                dgv.DataSource = null;
                total = 0;
                return;
            }
            SearchArg.DictType = typeData.TypeNo;
            SearchArg.SearchVal = tsTxtSearch.Text;
            SearchArg.PageNo = pageNo;
            SearchArg.PageSize = pageSize;
            var ret = BLLObj.GetList(SearchArg);
            total = ret.count;
            dgv.DataSource = ret.data;
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            var dictType = dgvDictType.GetSelectedClassData<Dict_CommonType>();
            if (dictType == null)
            {
                ShowMessage("选择分类");
                return;
            }
            FrmDictEdit frm = new FrmDictEdit();
            frm.Text += $" {dictType.TypeCaption}";
            var saveModel = new Dict_Common
            {
                DictValueOrderNo = dgv.RowCount + 1,
                DictType = dictType.TypeNo,
                DictTypeName = dictType.TypeCaption,
                IsEnable = true
            };

            var ret = frm.ShowDialog(new EditFormArgs
            {
                SaveHandle = m => BLLObj.Add(m as Dict_Common),
                SetModel = saveModel,
            });
            if (ret)
            {
                saveModel.Id = ret.data;
                dgv.AddDataSource(saveModel);
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnEdit_Click(object sender, EventArgs e)
        {
            var selDict = dgv.GetSelectedClassData<Dict_Common>();
            if (selDict == null)
            {
                ShowMessage("没有选中记录");
                return;
            }

            var frm = new FrmDictEdit();
            frm.Text += " 修改";
            var arg = new EditFormArgs
            {
                SaveHandle = m => BLLObj.Update(m as Dict_Common),
                SaveModel = selDict,
                SetModel = selDict,
                IsEditShow = true
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                ShowMessage(ret.msg);
                dgv.Refresh();
            }
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            var selModel = dgv.GetSelectedClassData<Dict_Common>();
            if (selModel == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            if (!ShowQuestion("确实要删除当前选中记录吗？", "删除确认"))
            {
                return;
            }
            var ret = BLLObj.Del(selModel);
            if (ret)
            {
                dgv.DelDataSource(selModel);
            }
            ShowMessage(ret.msg);
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void tsTxtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RefreshData();
            }
        }

        private void tsBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsBtnTypeSearch_Click(object sender, EventArgs e)
        {
            RefreshType();
        }

        private void tsTxtTypeSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RefreshType();
            }
        }
    }
}